package cn.xzqwjw.taskmanager.controller.api_public;

import cn.xzqwjw.taskmanager.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 提供一些公用的、不需要进行jwt验证的接口地址-文章相关
 *
 * @author wjw
 */
@RestController
@RequestMapping("/api-public/article")
public class PublicArticleController {

  private final ArticleService articleService;

  @Autowired
  public PublicArticleController(ArticleService articleService) {
    this.articleService = articleService;
  }

  /**
   * 根据文章id查询得到某个文章的详情
   */
  @GetMapping("/{id}")
  public String queryById(
      // @ApiParam("文章id")
      @NotNull(message = "id不能为空")
      @Min(value = 1, message = "id必须大于0")
      @PathVariable Integer id) {
    return articleService.queryById(id);
  }

  /**
   * 根据一组文章分类Id，得到文章分页数据
   * url类似：/api-public/article/list-page?PageCurrent=2&PageSize=3&CategoryIds=4,6,7
   */
  @GetMapping("/list-page")
  public String listPage(
      // @ApiParam("当前页码")
      @RequestParam(value = "PageCurrent", defaultValue = "1") Integer pageCurrent,
      // @ApiParam("每页文章数")
      @RequestParam(value = "PageSize", defaultValue = "20") Integer pageSize,
      // @ApiParam("文章类别id列表")
      @NotBlank(message = "文章类别ids不能为空")
      @RequestParam(value = "CategoryIds") String categoryIds) {
    return articleService.listPage4Front(pageCurrent, pageSize, categoryIds);
  }

  /**
   * 某普通编辑根据标题关键字词搜索文章，返回分页数据
   * url类似：/api-public/article/search?PageCurrent=2&PageSize=3&Keyword=三体
   */
  @GetMapping("/search")
  public String search(
      // @ApiParam("当前页码")
      @RequestParam(value = "PageCurrent", defaultValue = "1") Integer pageCurrent,
      // @ApiParam("每页文章数")
      @RequestParam(value = "PageSize", defaultValue = "20") Integer pageSize,
      // @ApiParam("搜索关键字词")
      @NotBlank(message = "搜索关键字词不能为空")
      @RequestParam(value = "Keyword") String keyword) {
    return articleService.search4Front(pageCurrent, pageSize, keyword);
  }

}
